package RecipientsGroups;

sub new 
{
    my ($class) = @_;
    my $self    = {};

    $self->{recipient_id}  = -1;
    $self->{group_id}      = -1;
    $self->{realm_id}      = -1;

    bless $self, 'RecipientsGroups';
    return $self;
}

#sub newFromDBbyId
#{
#   my ($class, $recipients_groups_id)	= @_;
#   my $self = {};
#   
#   my $stmt = "SELECT id, recipient_id, group_id, active 
#               FROM   recipients_groups 
#               WHERE  id = $recipients_groups_id;";
#}


#  -- Given realm_id and group_id, returns all recipients (with details)
sub get_recipients
{
   my $self = shift;
   
   my $kit_db  = new KIT_DB();
   
   my $sql     = sprintf ("
                  SELECT   R.id, f_name, l_name, R.email_addr, R.company_name
                  FROM     recipients R, recipients_groups RG, groups G 
                  WHERE    G.id = %d
                  AND      R.realm_id = %d 
                  AND      G.id = RG.group_id 
                  AND      R.id = RG.recipient_id;", $self->{group_id}, $self->{realm_id});

   my @recipients = $kit_db->GetMultipleRows($sql);
   return @recipients;
}

#  -- Given recipient_id and group_id, this function returns the recipients_groups 
#     table id (primary_key) of the record
sub get_id
{
   my $self    = shift;

   my $kit_db  = new KIT_DB();
   my $stmt    = sprintf("
                     SELECT id 
                     FROM   recipients_groups 
                     WHERE  recipient_id = %d 
                     AND    group_id = %d", $self->{recipient_id}, $self->{group_id});

   my $ret_val = $kit_db->GetSingleValue($stmt);
   
   $Utils::logger->info($stmt . " (" . $ret_val . ")");
   
   return $ret_val;
}
1;